interface AnimalInterface {
  name: string;
}

class Animal implements AnimalInterface {
  name: string;
  constructor(name: string) {
    this.name = name;
  }
}

class Cat extends Animal implements AnimalInterface {
  name: string;
  constructor(name: string) {
    super(name);
  }
  code = 0;
}
class Dog extends Animal implements AnimalInterface {
  name: string;
  constructor(name: string) {
    super(name);
  }
  code = 1;
}
let cat1 = new Cat('大白');
let dog1 = new Dog('大黄');
console.log(cat1);
console.log(dog1);

// 父类收窄为子类

function isSwim(animal: Animal): void {
  if ((animal as Cat | Dog).code === 0) {
    console.log('不会游泳');
  } else {
    console.log('会游泳');
  }
}

isSwim(cat1);
